var happyReaderLink = {
    linkData:{
        main:[],
        down:[],
        other:[]
    },
    inited:false,
    getData:function(contentDom){
        function getDomain(){
            var removeTag = ["com","cn","net","org","cc"],
            host = location.hostname,
            hosts = host.split(".");
            for(var idx = hosts.length - 1; idx >=0 ; idx--){
                if (!$.inArray(hosts[idx],removeTag)){
                    return hosts[idx];
                }
            }
            return host;
        }
        
        function checkDownFile(fileName){
            var downTag = ["rar", "zip", "7z", "gz", "tar", "epub", "pdf", "txt", "exe", "apk", "mp3", "wma", "rm", "mpg"],
            ext = fileName.split(".").pop();
            return $.inArray(ext, downTag)
        }
        
        function uniqueLink(links){
            var resLinksIdx = [],resLinks = [],res = [];
            for(var key in links)
                resLinks = $.mergeArray(resLinks, links[key]);
            
            //unique by url
            for(var i=0,len=resLinks.length;i<len;i++){
                var item = resLinks[i];
                if (!$.inArray(item.href,resLinksIdx)){
                    res.push(item);
                    resLinksIdx.push(item.href);
                }
            }
            return res;
        }

        var mainHost = getDomain();
        var tmpLinks = contentDom.getElementsByTagName("a");
        var mainLinks = {}, downLinks = {}, otherLinks = {};
        for(var i in tmpLinks){
            var link = tmpLinks[i];
            //filter "#","#nogo","javascript","mailto"
            if (link.href && link.href.search(/#|#nogo|javascript|mailto/i) == -1 && link.firstChild != null && link.firstChild.nodeType == 3){
			    var host = link.href.split("/")[2] , linkText = link.innerHTML.replace(/<.*?>/g,"").trim();
                if (!host || !linkText)continue;
                if (checkDownFile(linkText)){
                    if (downLinks[linkText] === undefined)
                        downLinks[linkText] = [];
                    downLinks[linkText].push({"href":link.href,"text":linkText});
                }
                else if (host.indexOf(mainHost) >= 0){
                    //get mainDomain links
                    if(mainLinks[host] === undefined)
                        mainLinks[host] = [];
                    mainLinks[host].push({"href":link.href,"text":linkText});
                }
                else{
                    if(otherLinks[host] === undefined)
                        otherLinks[host] = [];
                    otherLinks[host].push({"href":link.href,"text":link.firstChild.nodeValue});
                }
            }
        }
        this.linkData.down = uniqueLink(downLinks);
        this.linkData.main = uniqueLink(mainLinks);
        this.linkData.other = uniqueLink(otherLinks);
    },
    init:function(){
        $('#happyReaderHeaderAD').html(_m("otherModeHeaderAD"));
		happyReader.setUserConfig("viewMode","link");
        if (this.inited)return ;
        this.inited = true;
        
        var linkCanvasID = "linkCanvas", 
            linkCanvas = $('#'+linkCanvasID);
        
        var html = "<table width=100% cellspacing=0 cellpadding=0 border=0>";
        if (this.linkData.down.length > 0) {
            //add download links
            this.linkData.down.forEach(function(item,idx){
                var gray = (idx%2 == 1) ? "style='background:#ddd;'" : ""; 
                html += "<tr "+gray+"><td width=40% title='"+item.text+"'><span class='downlink'>"+item.text.slice(0,80)+"</span></td><td width=59%><a href='"+ item.href +"' title="+item.href+">"+item.href.slice(0,90)+"</a></td></tr>";
            })
            html+="<tr><td colspan=2><hr/></td></tr>";
        }
        if (this.linkData.main.length > 0) {
            //add mainDomain links
            this.linkData.main.forEach(function(item,idx){
                var gray = (idx%2 == 0) ? "style='background:#ddd;'" : ""; 
                html += "<tr "+gray+"><td width=40% title='"+item.text+"'>"+item.text.slice(0,80)+"</td><td width=59%><a href='"+ item.href +"' title="+item.href+">"+item.href.slice(0,90)+"</a></td></tr>";
            })
            html+="<tr><td colspan=2><hr/></td></tr>";
        }
        //add otherDomain links
        this.linkData.other.forEach(function(item,idx){
            var gray = (idx%2 == 0) ? "style='background:#ddd;'" : ""; 
            html += "<tr "+gray+"><td width=40% title='"+item.text+"'>"+item.text.slice(0,80)+"</td><td width=59%><a href='"+ item.href +"' title="+item.href+">"+item.href.slice(0,90)+"</a></td></tr>";
        })
        html+="</table>";
        
        $('#'+linkCanvasID).css({left:0}).html(html);   
    },
	checkModeValid:function(){
        return this.linkData.main.length > 0;
    }
};